<?php

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

require_once  __DIR__ . '/includes/authenticate.php';

include_once __DIR__ . '/db_bb_connect.php';

include __DIR__ . '/mainpage.php';

?>
    <div class="container-fluid">
    
    <div class="row">
      <div class="col-md-3"></div>
      <div class="col-md-6"> 
        <br>
      <?php 

if (isset($_POST['articleID'])) {

  $setarticleID = $_POST['articleID'];

  $sql = "SELECT title, text FROM articles WHERE ID = '$setarticleID'";
  $result = $db->query($sql);
  $row = $result->fetch_assoc();
  $settitle = htmlspecialchars($row['title']);
  $settext = $row['text']; 

?>
    <div class="text-center"></div>
        <h1>Editing Article "<?=$settitle?>" </h1>
        <p>
        <a class="btn btn-default btn-sm" href="./articles_archive.php">Back to Archive</a>
        <a class="btn btn-default btn-sm" href="./article_add.php">Add New Article</a>
        </p>

    <form>

      <input type="hidden" name="ID" value="<?= $setarticleID ?>">



<div class="form-group">
  <input type="text" name="title" class="form-control" value="<?= $settitle ?>">
</div>

<div class="form-group centered">
<textarea id="mytextarea" name="text" class="form-control">

<?= $settext ?>



</textarea>
</div>
<br>
<div id="success_message"></div>
    <br>
    <button class="btn btn-success btn-lg" id="submit" name="submit">Update</button>
      </form>

      <?php } ?>
<br>
</div>
<div class="col-md-3"></div>

      </div>
      
    </div>

 
  <script type="text/javascript" src="./js/tinymce/tinymce.min.js"></script>

    <script type="text/javascript">
  tinymce.init({
        selector: "#mytextarea",
        theme: "modern",
        width: 680,
        height: 300,
        plugins: [
         "advlist autolink link image lists charmap print preview hr anchor pagebreak",
         "searchreplace wordcount visualblocks visualchars insertdatetime media nonbreaking",
         "table contextmenu directionality emoticons paste textcolor responsivefilemanager code"
        ],
        toolbar1: "undo redo | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | styleselect",
        toolbar2: "| responsivefilemanager | link unlink anchor | image media | forecolor backcolor  | print preview code ",
        image_advtab: true,
        relative_urls: false,
        image_caption: true,
   
        external_filemanager_path:"filemanager/",
        filemanager_title:"Responsive Filemanager" ,
        external_plugins: { "filemanager" : "../../filemanager/plugin.min.js"}
  });

  
 
  $('#submit').on('click', function(event){

    tinymce.triggerSave();

    event.preventDefault();
    var form_data = $(this.form).serialize();

    $.ajax({
    url: "./includes/handlers/article_edit_handler.php",
    method: "POST",
    data: form_data,
    beforeSend:function(){
     $('#submit').val('Submitting...');
    },
    success:function(data){
     if(data != '')
     {
      $('#success_message').html(data);
      $('#submit').attr("disabled", false);
      $('#submit').val('Update');
     }
    }, 
    error: function (xhr, status, error) {
          alert(xhr.responseText); 
    }
    });
      setInterval(function(){
        $('#success_message').html('');
    }, 5000);

    });


  </script>

